﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using RTracker.Models.ErrorLog;

namespace RTracker.Models.Common
{
    public class MUserSession
    {
        public CStatus CreateUserSession(CAuthenticationContext objCAuthenticationContext, ref CUserSession objCUserSession)
        {
            CStatus oCStatus = new CStatus();
            if (!CCommonCollection.ConnectionString.Equals(string.Empty))
            {
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {

                        objCUserSession.Device = HttpContext.Current.Application["Browser"].ToString();
                        objCUserSession.IpAddress = HttpContext.Current.Application["IpAddress"].ToString();
                        objCUserSession.OperatingSystem = HttpContext.Current.Application["OS"].ToString();
                        objCUserSession.Location = HttpContext.Current.Application["Location"].ToString();
                        objCUserSession.ApplicationUsed = HttpContext.Current.Application["Name"].ToString();

                        con.Open();

                        SqlCommand cmd = new SqlCommand("r_create_user_session_procedure", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@user_id", SqlDbType.Int).Value = objCUserSession.UserId;
                        cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCUserSession.OrganizationId;
                        cmd.Parameters.Add("@device", SqlDbType.NVarChar).Value = objCUserSession.Device;
                        cmd.Parameters.Add("@ip_address", SqlDbType.NVarChar).Value = objCUserSession.IpAddress;
                        cmd.Parameters.Add("@operating_system", SqlDbType.NVarChar).Value = objCUserSession.OperatingSystem;
                        cmd.Parameters.Add("@application_used", SqlDbType.NVarChar).Value = objCUserSession.ApplicationUsed;
                        cmd.Parameters.Add("@version", SqlDbType.NVarChar).Value = objCUserSession.Version;
                        cmd.Parameters.Add("@location", SqlDbType.NVarChar).Value = objCUserSession.Location;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCUserSession.UserId;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@session_key", SqlDbType.VarChar,50).Value = null;
                        cmd.Parameters["@session_key"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                            objCUserSession.SessionKey = cmd.Parameters["@session_key"].Value.ToString();
                            objCUserSession.UserSessionId = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString());
                            
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                            //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            }
            else
            {
                oCStatus.Success = false;
                oCStatus.StatusDetails = CAppErrorsConstants.ConStringNotFound;
            }

            return oCStatus;
        }

        public CStatus EndUserSession(CAuthenticationContext objCAuthenticationContext, ref CUserSession objCUserSession)
        {
            CStatus oCStatus = new CStatus();
            if (!CCommonCollection.ConnectionString.Equals(string.Empty))
            {
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();

                        SqlCommand cmd = new SqlCommand("r_end_user_session_procedure", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@session_key", SqlDbType.VarChar).Value = objCUserSession.SessionKey;
                        cmd.Parameters.Add("@user_session_id", SqlDbType.Int).Value = objCUserSession.UserSessionId;
                        cmd.Parameters.Add("@user_id", SqlDbType.Int).Value = objCUserSession.UserId;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCUserSession.UserId;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                            //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            }
            else
            {
                oCStatus.Success = false;
                oCStatus.StatusDetails = CAppErrorsConstants.ConStringNotFound;
            }

            return oCStatus;
        }
    }
}